/**
 *
 * Copyright (c) 2011 TLOSS. All rights reserved.
 * Created on Apr 22, 2011
 *
 */
package com.tloss.database.db2obj.classpath;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.tloss.database.db2obj.classpath.except.InvalidConfiguration;

/**
 * @author Tran Tung
 *
 */
public class ConfigurationImpl extends Configuration {

	protected List<String> processSources(Element sources) {
		List<String> result = new ArrayList<String>();
		NodeList sourcesNodeList = sources.getChildNodes();
		for (int i = 0; i < sourcesNodeList.getLength(); i++) {
			Node node = sourcesNodeList.item(i);
			if (node.getNodeType() == Node.ELEMENT_NODE
					&& ((Element) node).getTagName().equals("source")) {
				String dir = ((Element) node).getAttribute("dir");
				if (dir != null && !"".equals(dir.trim())) {
					result.add(dir);
				}
			}
		}
		return result;
	}

	protected List<String> processLibraries(Element libraries) {
		List<String> result = new ArrayList<String>();
		NodeList sourcesNodeList = libraries.getChildNodes();
		for (int i = 0; i < sourcesNodeList.getLength(); i++) {
			Node node = sourcesNodeList.item(i);
			if (node.getNodeType() == Node.ELEMENT_NODE
					&& ((Element) node).getTagName().equals("library")) {
				String jar = ((Element) node).getAttribute("jar");
				if (jar != null && !"".equals(jar.trim())) {
					result.add(jar);
				}
			}
		}
		return result;
	}

	@Override
	public void readConfig(InputStream inputStream) throws InvalidConfiguration {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		try {
			builder = factory.newDocumentBuilder();
			Document document = builder.parse(inputStream);
			Element project = document.getDocumentElement();
			projectName =  project.getAttribute("name");
			compilerTarget = project.getAttribute("compilerTarget");
			compilerSource = project.getAttribute("compilerSource");
			NodeList projectNodeList = project.getChildNodes();
			for (int i = 0; i < projectNodeList.getLength(); i++) {
				Node node = projectNodeList.item(i);
				if (node.getNodeType() == Node.ELEMENT_NODE
						&& ((Element) node).getTagName().equals("sources")) {
					sources = processSources((Element) node);
				}
				if (node.getNodeType() == Node.ELEMENT_NODE
						&& ((Element) node).getTagName().equals("libraries")) {
					libraries = processLibraries((Element) node);
				}
			}
		} catch (ParserConfigurationException e) {
			throw new InvalidConfiguration(e);
		} catch (SAXException e) {
			throw new InvalidConfiguration(e);
		} catch (IOException e) {
			throw new InvalidConfiguration(e);
		}

	}

}
